package com.wjjun.presenter;

import android.databinding.ObservableList;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.wjjun.basemvpvm.view.BasePresenter;
import com.wjjun.customview.views.NoScrollViewPager;
import com.wjjun.viewmodel.TabItemViewModel;
import com.wjjun.viewmodel.TabViewPagerViewModel;

/**
 * viewGroup使用的presenter
 * Created by 67535 on 2018/4/29.
 */
public class TabViewPagerPresenter implements BasePresenter {

    private ViewPager viewPager;
    private TabLayout tabLayout;
    private TabViewPagerViewModel viewModel;

    private FragmentManager fragmentManager;
    private FragmentPagerAdapter fragmentPagerAdapter;

    private ViewPager.OnPageChangeListener onPageChangeListener;

    public TabViewPagerPresenter(FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
    }

    public void bindTabViewModel(TabViewPagerViewModel tabViewModel) {
        this.viewModel = tabViewModel;
        bindData();
    }

    private void bindData() {
        if (viewModel == null) {
            return;
        }
        viewModel.tabs.addOnListChangedCallback(new ObservableList.OnListChangedCallback<ObservableList<TabItemViewModel>>() {
            @Override
            public void onChanged(ObservableList<TabItemViewModel> tabItemViewModels) {
                onTabsChange();
            }

            @Override
            public void onItemRangeChanged(ObservableList<TabItemViewModel> tabItemViewModels, int i, int i1) {
                onTabsChange();
            }

            @Override
            public void onItemRangeInserted(ObservableList<TabItemViewModel> tabItemViewModels, int i, int i1) {
                onTabsChange();
            }

            @Override
            public void onItemRangeMoved(ObservableList<TabItemViewModel> tabItemViewModels, int i, int i1, int i2) {
                onTabsChange();
            }

            @Override
            public void onItemRangeRemoved(ObservableList<TabItemViewModel> tabItemViewModels, int i, int i1) {
                onTabsChange();
            }
        });
    }

    private void onTabsChange() {
        if (fragmentPagerAdapter != null) {
            fragmentPagerAdapter.notifyDataSetChanged();
        }
        if (viewPager != null && viewPager instanceof NoScrollViewPager) {
            ((NoScrollViewPager) viewPager).setNoScroll(viewModel.tabs.size() <= 1);
        }
    }

    public void bindTabLayout(TabLayout tabLayout) {
        this.tabLayout = tabLayout;
        initView();
    }

    public void bindViewPager(ViewPager viewPager) {
        this.viewPager = viewPager;
        initView();
    }

    private void initView() {
        if (viewPager == null || tabLayout == null) {
            return;
        }

        tabLayout.setupWithViewPager(viewPager);
        initViewPager();
    }

    private void initViewPager() {
        if (fragmentPagerAdapter == null) {
            fragmentPagerAdapter = new FragmentPagerAdapter(fragmentManager) {

                @Override
                public Fragment getItem(int position) {
                    return viewModel.tabs.get(position).fragment;
                }

                @Override
                public int getCount() {
                    if (viewModel == null) {
                        return 0;
                    }
                    return viewModel.tabs.size();
                }

                @Override
                public CharSequence getPageTitle(int position) {
                    return viewModel.tabs.get(position).title;
                }
            };
        }

        viewPager.setAdapter(fragmentPagerAdapter);
        if (onPageChangeListener != null) {
            viewPager.removeOnPageChangeListener(onPageChangeListener);
            viewPager.addOnPageChangeListener(onPageChangeListener);
        }
    }

    public void switchTab(int position) {
        if (viewPager != null) {
            viewPager.setCurrentItem(position);
        }
    }


    @Override
    public void onClick(View view) {

    }

    public ViewPager.OnPageChangeListener getOnPageChangeListener() {
        return onPageChangeListener;
    }

    public void setOnPageChangeListener(ViewPager.OnPageChangeListener onPageChangeListener) {
        this.onPageChangeListener = onPageChangeListener;
    }
}
